<script>
var ajax = function(obj,$){

    var loadIndex = layer.load(0, {shade: false});

    var that = this;

    var url = obj.url ? obj.url : '';
    var data = obj.data ? obj.data : '';
    var type = obj.type ? obj.type : ''; // 请求方式
    var async = obj.async ? obj.async : true; // 是否异步
    var dataType = obj.dataType ? obj.dataType : ''; // 请求类型

    var header = obj.header ? obj.header : {Accept: "application/json; charset=utf-8"};
    var responseType = obj.responseType ? obj.responseType : '';
    var success = obj.success ? obj.success : '';
    var fail = obj.fail ? obj.fail : '';
    var error = obj.error ? obj.error : '';
    var complete = obj.complete ? obj.complete : '' ;

    var objs = {};
    if(url){
        objs.url = url;
    }

    if (data) {
        objs.data = data;
    }

    if (async) {
        objs.async = async;
    }

    if (header) {
        objs.headers = header;
    }

    if (type) {
        objs.type = type;
    }

    if (dataType) {
        objs.dataType = dataType;
    }

    if (responseType) {
        objs.responseType = responseType;
    }

    objs.success = function (res) {
        msgtext(res.code,res.msg);
        success ? success(res) : '';
        layer.close(loadIndex);
    }

    objs.fail = function (res) {
    fail ? fail(res) : '';
    layer.close(loadIndex);
    }

    objs.complete = function (res) {
    complete ? complete(res) : '';
    layer.close(loadIndex);
    }

    objs.error = function (res) {
    error ? error(res) : '';
    layer.close(loadIndex);
    }

    $.ajax(objs);
};

var msgtext = function(is = 1,text){
    if(is){

        if(top){
            top.layer.msg(text, {icon: 1});
        }else{
            layer.msg(text, {icon: 1});
        }
        
    }else{

        if(top){
            top.layer.confirm(text, {
                title:'',
                btn: ['关闭'] //按钮
            });
        }else{
            layer.confirm(text, {
                title:'',
                btn: ['关闭'] //按钮
            });
        }

    }
}
</script>